package com.zzh.takeoutapp.view.goods;


import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.florent37.viewanimator.AnimationListener;
import com.github.florent37.viewanimator.ViewAnimator;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.activity.OrderCarDatelisActivity;
import com.zzh.takeoutapp.entity.FoodBean;

import java.util.List;

public class AddWidget extends FrameLayout {

    private View sub;
    private TextView tv_count;
    private long count;
    private AddButton addbutton;
    private boolean sub_anim, circle_anim;
    private FoodBean foodBean;
    private List<FoodBean> beanList;
    private BaseQuickAdapter adapter;

    public interface OnAddClick {

        void onAddClick(View view, FoodBean fb, FoodBean foodBean1);

        void onSubClick(FoodBean fb, FoodBean foodBean1);
    }

    private OnAddClick onAddClick;

    public AddWidget(@NonNull Context context) {
        super(context);
    }

    public AddWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(context, R.layout.view_addwidget, this);
        addbutton = findViewById(R.id.addbutton);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AddWidget);
        for (int i = 0; i < a.getIndexCount(); i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.AddWidget_circle_anim:
                    circle_anim = a.getBoolean(R.styleable.AddWidget_circle_anim, false);
                    break;
                case R.styleable.AddWidget_sub_anim:
                    sub_anim = a.getBoolean(R.styleable.AddWidget_sub_anim, false);
                    break;
            }

        }
        a.recycle();
        sub = findViewById(R.id.iv_sub);
        tv_count = findViewById(R.id.tv_count);
        addbutton.setAnimListner(new AddButton.AnimListner() {
            @Override
            public void onStop() {
                if (count == 0) {
                    ViewAnimator.animate(sub)
                            .translationX(addbutton.getLeft() - sub.getLeft(), 0)
                            .rotation(360)
                            .alpha(0, 255)
                            .duration(300)
                            .interpolator(new DecelerateInterpolator())
                            .andAnimate(tv_count)
                            .translationX(addbutton.getLeft() - tv_count.getLeft(), 0)
                            .rotation(360)
                            .alpha(0, 255)
                            .interpolator(new DecelerateInterpolator())
                            .duration(300)
                            .start();

                }
                count++;
                if (foodBean.getSpecsX() != null && foodBean.getSpecsX().size() > 0) {
                    String[] split = foodBean.getProduct_id().split(foodBean.getSpecsX().get(0).getSpec_id());
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(split[0])) {
                            beanList.get(i).setSelectCount(beanList.get(i).getSelectCount() + 1);
                            tv_count.setText(count + "");
                            foodBean.setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onAddClick(addbutton, beanList.get(i), foodBean);
                            }
                        }
                    }
                } else if (foodBean.getSpecification() != null && foodBean.getSpecification().size() > 0) {
                    String[] split = foodBean.getProduct_id().split(foodBean.getSpecification().get(0).getKey());
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(split[0])) {
                            beanList.get(i).setSelectCount(beanList.get(i).getSelectCount() + 1);
                            tv_count.setText(count + "");
                            foodBean.setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onAddClick(addbutton, beanList.get(i), foodBean);
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(foodBean.getProduct_id())) {
                            tv_count.setText(count + "");
                            beanList.get(i).setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onAddClick(addbutton, beanList.get(i), foodBean);
                            }
                        }
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        sub.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (count == 0) {
                    return;
                }
                if (count == 1 && sub_anim) {
                    subAnim();
                }
                count--;
                if (foodBean.getSpecsX() != null && foodBean.getSpecsX().size() > 0) {
                    String[] split = foodBean.getProduct_id().split(foodBean.getSpecsX().get(0).getSpec_id());
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(split[0])) {
                            tv_count.setText(count == 0 ? "1" : count + "");
                            beanList.get(i).setSelectCount(beanList.get(i).getSelectCount() - 1);
                            foodBean.setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onSubClick(beanList.get(i), foodBean);
                            }
                        }
                    }
                } else if (foodBean.getSpecification() != null && foodBean.getSpecification().size() > 0) {
                    String[] split = foodBean.getProduct_id().split(foodBean.getSpecification().get(0).getKey());
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(split[0])) {
                            tv_count.setText(count == 0 ? "1" : count + "");
                            beanList.get(i).setSelectCount(beanList.get(i).getSelectCount() - 1);
                            foodBean.setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onSubClick(beanList.get(i), foodBean);
                            }
                        }
                    }
                } else {
                    for (int i = 0; i < beanList.size(); i++) {
                        if (beanList.get(i).getProduct_id().equals(foodBean.getProduct_id())) {
                            tv_count.setText(count == 0 ? "1" : count + "");
                            beanList.get(i).setSelectCount(count);
                            if (onAddClick != null) {
                                onAddClick.onSubClick(beanList.get(i), foodBean);
                            }
                        }

                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
    }

    private void subAnim() {
        ViewAnimator.animate(sub)
                .translationX(0, addbutton.getLeft() - sub.getLeft())
                .rotation(-360)
                .alpha(255, 0)
                .duration(300)
                .interpolator(new AccelerateInterpolator())
                .andAnimate(tv_count)
                .onStop(new AnimationListener.Stop() {
                    @Override
                    public void onStop() {
                        if (circle_anim) {
                            addbutton.expendAnim();
                        }
                    }
                })
                .translationX(0, addbutton.getLeft() - tv_count.getLeft())
                .rotation(-360)
                .alpha(255, 0)
                .interpolator(new AccelerateInterpolator())
                .duration(300)
                .start();

    }


    public void setData(BaseQuickAdapter mContext, OnAddClick onAddClick, List<FoodBean> list, FoodBean bean) {
        this.adapter = mContext;
        this.foodBean = bean;
        this.beanList = OrderCarDatelisActivity.foodBeanDatas;
        if (bean.getSpecsX() != null && bean.getSpecsX().size() > 0) {
            String[] split = bean.getProduct_id().split(bean.getSpecsX().get(0).getSpec_id());
            for (int i = 0; i < beanList.size(); i++) {
                if (beanList.get(i).getProduct_id().equals(split[0])) {
                    count = bean.getSelectCount();
                }
            }
        } else if (bean.getSpecification() != null && bean.getSpecification().size() > 0) {
            String[] split = bean.getProduct_id().split(bean.getSpecification().get(0).getKey());
            for (int i = 0; i < beanList.size(); i++) {
                if (beanList.get(i).getProduct_id().equals(split[0])) {
                    count = bean.getSelectCount();
                }
            }
        } else {
            for (int i = 0; i < beanList.size(); i++) {
                if (beanList.get(i).getProduct_id().equals(foodBean.getProduct_id())) {
                    count = beanList.get(i).getSelectCount();
                }
            }
        }

        this.onAddClick = onAddClick;
        if (count == 0) {
            sub.setAlpha(0);
            tv_count.setAlpha(0);
        } else {
            sub.setAlpha(1f);
            tv_count.setAlpha(1f);
            tv_count.setText(count + "");
        }
    }

    public void setState(long count) {
        addbutton.setState(count > 0);
    }

    public void expendAdd(long count) {
        this.count = count;
        tv_count.setText(count == 0 ? "1" : count + "");
       /* if (count == 0) {
            subAnim();
        }*/
    }
}
